package control;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import connect.BaseValue;
import connect.HttpConnect;
import entity.ClipMessage;
import view.MainPage;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ClipBoardControl {
    public static final java.util.List<ClipMessage> messages = new ArrayList<>();
    public Timer timer;
    ClipBoardControl(){
        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (MainPage.username.length() > 0 && MainPage.token.length() > 0){
                    sync(MainPage.username, "pc");
                }
            }
        });
        timer.start();
    }

    public void sync(String token, String device){
        Map<String, String> params = new HashMap<>();
        params.put("device", device);
        params.put("token", token);
        HttpConnect connect = new HttpConnect();
        String result = connect.doPost(BaseValue.BASE_URL + "/sync", null, params);
        java.util.List<String> array = JSONObject.parseArray(result, String.class);
        if (array.size() > 0){
            for (String a: array
                 ) {
                ClipMessage clipMessage = new ClipMessage(a, "android", token, "");
                messages.add(clipMessage);
            }
        }
    }
    public String upload(String token, ClipMessage clipMessage){
        Map<String, String> params = new HashMap<>();
        params.put("device", clipMessage.getDevice());
        params.put("content", clipMessage.getContent());
        params.put("token", token);
        HttpConnect connect = new HttpConnect();
        String result = connect.doPost(BaseValue.BASE_URL + "/upload", null, params);
        JSONObject object = JSONObject.parseObject(result);
        if (object.getInteger("code") == 0){
            return object.getString("token");
        }
        return "";
    }

    public java.util.List<ClipMessage> getMessages(){
        java.util.List<ClipMessage> list = new ArrayList(messages);
        messages.clear();
        return list;
    }
    /**
     * 把文本设置到剪贴板（复制）
     */
    public static void setClipboardString(String text) {
        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 封装文本内容
        Transferable trans = new StringSelection(text);
        // 把文本内容设置到系统剪贴板
        clipboard.setContents(trans, null);
    }

    /**
     * 从剪贴板中获取文本（粘贴）
     */
    public static String getClipboardString() {
        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        // 获取剪贴板中的内容
        Transferable trans = clipboard.getContents(null);

        if (trans != null) {
            // 判断剪贴板中的内容是否支持文本
            if (trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                try {
                    // 获取剪贴板中的文本内容
                    return (String) trans.getTransferData(DataFlavor.stringFlavor);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }
}
